package com.weipeep.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.weipeep.common.base.BaseController;
import com.weipeep.common.utils.Result;
import com.weipeep.sys.CurrentLinkConstant;
import com.weipeep.sys.domain.BaoJiaDO;
import com.weipeep.sys.domain.WebUserDO;
import com.weipeep.sys.domain.XunJiaDO;
import com.weipeep.sys.service.*;
import com.weipeep.sys.vo.ReceiveBaojiaQuery;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class MemberCenterController extends BaseController {
    @Resource
    private WebUserService webUserService;
    @Resource
    private XunJiaService xunJiaService;
    @Resource
    private BaoJiaService baoJiaService;
    @Resource
    private ProductService productService;
    @Resource
    private DemandService demandService;
    @RequestMapping("/member/welcome")
    public String toWelcome(Model model){
        if (getUser().getUserType()==2){
            model.addAttribute("focus", CurrentLinkConstant.admin);
            return "admincenter";
        }
        model.addAttribute("focus", CurrentLinkConstant.welcome);
        WebUserDO webUserDO = webUserService.selectById(getUserId());
        model.addAttribute("user",webUserService.selectById(getUserId()));
        model.addAttribute("userName",webUserDO.getUserName());
        model.addAttribute("webSite","http://www.01bearing.com/front/"+webUserDO.getUserAccount());
        model.addAttribute("memberType",webUserDO.getMemberType());

        Wrapper xunJiaDOWrapper = new EntityWrapper<>();
        xunJiaDOWrapper.where("user_id={0}",webUserDO.getId());
        xunJiaDOWrapper.andNew("xun_jia_type={0}",1);
        Integer myxunjia = xunJiaService.selectCount(xunJiaDOWrapper);
        model.addAttribute("myxunjia",myxunjia);

        ReceiveBaojiaQuery mybaojiaQuery = new ReceiveBaojiaQuery();
        mybaojiaQuery.setBaojiaUserId(webUserDO.getId());
        Integer mybaojia = baoJiaService.myBaojiaCount(mybaojiaQuery);
        model.addAttribute("mybaojia",mybaojia);

        Wrapper mykucunDOWrapper = new EntityWrapper<>();
        mykucunDOWrapper.where("user_id={0}",webUserDO.getId());
        Integer mykucun = productService.selectCount(mykucunDOWrapper);
        model.addAttribute("mykucun",mykucun);
        Wrapper mycaigouDOWrapper = new EntityWrapper<>();
        mycaigouDOWrapper.where("user_id={0}",webUserDO.getId());
        Integer mycaigou = demandService.selectCount(mycaigouDOWrapper);
        model.addAttribute("mycaigou",mycaigou);

        ReceiveBaojiaQuery shoudaobaojiaQuery = new ReceiveBaojiaQuery();
        shoudaobaojiaQuery.setXunjiaUserId(webUserDO.getId());
        Integer shoudaobaojia = baoJiaService.receiveBaojiaCount(shoudaobaojiaQuery);
        model.addAttribute("shoudaobaojia",shoudaobaojia);

        Wrapper receivexunJiaDOWrapper = new EntityWrapper<>();
        receivexunJiaDOWrapper.where("to_user_id={0}",webUserDO.getId());
        Integer shoudaoxunjia = xunJiaService.selectCount(receivexunJiaDOWrapper);
        model.addAttribute("shoudaoxunjia",shoudaoxunjia);

        return "welcome";
    }
    @RequestMapping("/member/center")
    public String toMemberCenter(Model model){
            model.addAttribute("focus", CurrentLinkConstant.center);
            model.addAttribute("user",webUserService.selectById(getUserId()));
            return "membercenter";
    }

    /***
     *   public final static Integer center = 1;
         public final static Integer releasePurchase = 2;
         public final static Integer receiveOffer = 3;
         public final static Integer searchPurchase = 4;
         public final static Integer repertory = 5;
         public final static Integer sentOffer = 6;
     * @param model
     * @return
     */
    @RequestMapping("/member/release/purchase")
    public String toReleasePurchase(Model model){
        model.addAttribute("focus", CurrentLinkConstant.releasePurchase);
        return "releasepurchase";
    }
    @RequestMapping("/member/receive/offer")
    public String toReceiveOffer(Model model){
        model.addAttribute("focus", CurrentLinkConstant.receiveOffer);
        return "receiveoffer";
    }
    @RequestMapping("/member/my/xunjia")
    public String toMyXunjia(Model model){
        model.addAttribute("focus", CurrentLinkConstant.myXunjia);
        return "myxunjia";
    }
    @RequestMapping("/member/search/purchase")
    public String toSearchPurchase(Model model){
        model.addAttribute("focus", CurrentLinkConstant.searchPurchase);
        return "searchpurchase";
    }
    @RequestMapping("/member/repertory")
    public String toRepertory(Model model){
        model.addAttribute("focus", CurrentLinkConstant.repertory);
        return "repertory";
    }
    @RequestMapping("/member/sent/offer")
    public String toSentOffer(Model model){
        model.addAttribute("focus", CurrentLinkConstant.sentOffer);
        return "sentoffer";
    }
    @RequestMapping("/member/liulan/remai")
    public String liulanremai(Model model){
        model.addAttribute("focus", CurrentLinkConstant.liulanremai);
        return "liulanremai";
    }
    @RequestMapping("/member/shoudao/xunjia")
    public String shoudaoxunjia(Model model){
        model.addAttribute("focus", CurrentLinkConstant.shoudaoxunjia);
        return "shoudaoxunjia";
    }
    @RequestMapping("/member/update")
    @ResponseBody
    public Result memberUpdate(WebUserDO webUserDO){
        boolean flag = webUserService.updateById(webUserDO);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
}
